//
//  TimelineRetweetView.swift
//  01sinablog
//
//  Created by ZhangZiang on 15/12/21.
//  Copyright © 2015年 ZhangZiang. All rights reserved.
//

import UIKit
import SnapKit

class TimelineRetweetView: UIView {

    var bottomConstraint : Constraint?
    
    var retweet : Timeline? {
        didSet {
            textLabel.text = "@" + (retweet?.user?.name ?? "") + (retweet?.text ?? "")
            
            //给富文本
            if let text = textLabel.text {
            textLabel.attributedText = EmoticonManager.sharedEmoticonManager.textStringToEmoticonImageString(text, font: textLabel.font)
            }
            pictureView.imageURLs = retweet?.imageURLs
            
            self.bottomConstraint?.uninstall()
            //根据是否有配图更新约束
            if let urls = retweet?.imageURLs where urls.count > 0
            {
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).offset(TimelineCellMargin).constraint
                })
            }
            else
            {
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraint = make.bottom.equalTo(textLabel.snp_bottom).offset(TimelineCellMargin).constraint
                })
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        backgroundColor = UIColor(white: 0.93, alpha: 1)
    }

    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI()
    {
        addSubview(textLabel)
        addSubview(pictureView)
        
        textLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top)
            make.left.equalTo(self.snp_left).offset(TimelineCellMargin)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(textLabel.snp_bottom).offset(TimelineCellMargin)
            make.left.equalTo(textLabel.snp_left)
        }
        
        //底部的约束
        self.snp_makeConstraints { (make) -> Void in
            self.bottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).offset(TimelineCellMargin).constraint
        }
    }

    //MARK : 懒加载
    lazy var textLabel = UILabel(title: "转发微博", color: UIColor.darkGrayColor(), fontSize: 15, margin: TimelineCellMargin)
    
    lazy var pictureView : TimelineCellPictureView = TimelineCellPictureView()
}
